using System.IO;
using System.Xml.Serialization;

namespace TunesHelper
{
  public class Xml
  {
    public static void Serialize<T>(string filename, T data)
    {
      TextWriter writer = null;
      try
      {
        writer = new StreamWriter(filename);
        var serializer = new XmlSerializer(typeof(T));
        serializer.Serialize(writer, data);
      }
      finally
      {
        if (writer != null)
        {
          writer.Close();
        }
      }
    }

    public static T Deserialize<T>(string filename)
    {
      TextReader reader = null;
      T data;
      try
      {
        reader = new StreamReader(filename);
        var serializer = new XmlSerializer(typeof(T));
        data = (T)serializer.Deserialize(reader);
      }
      finally
      {
        if (reader != null)
        {
          reader.Close();
        }
      }
      return data;
    }
  }
}